package com.business.serviceimpl;

import com.business.bean.Role;
import com.business.bean.RoleMenu;
import com.business.bean.layuiBean.ResultPage;
import com.business.bean.layuiBean.VoPage;
import com.business.mapper.RoleMapper;
import com.business.service.RoleMenuService;
import com.business.service.RoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.business.service.RoleUserService;
import com.business.util.cacheUtil.JedisUtil;
import com.util.Respons.ResponseUtil;
import com.util.exception.CommonException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2019-01-08
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    JedisUtil jedisUtil;

    @Autowired
    RoleMenuService roleMenuService;

    @Autowired
    RoleUserService roleUserService;
    @Override
    public ResultPage<Role> findRoleByPage(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<Role> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Role> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public Map<String, Object> findRoleAll(VoPage voPage) {
        List<Role> roles = jedisUtil.getList("roles");
        if (roles!=null) {
            return ResponseUtil.getSuccessMap(roles, "操作成功");

        } else {
            roles=this.selectByMap(new HashMap<>());
            jedisUtil.setList("roles",roles,0);
            return ResponseUtil.getSuccessMap((roles), "操作成功");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertRole(String roleName, String menuId) {
        //从redis中获取缓存 如果角色存在 则提示  缓存角色信息 是为了用户管理中下拉框的角色
        List<Role> roles = jedisUtil.getList("roles");
         if(roles==null){
             roles=this.selectByMap(new HashMap<>());
             jedisUtil.setList("roles",roles,0);
         }
        roles.stream().forEach(s -> {
            if (s.getRoleName().equals(roleName)) {
                throw new CommonException("该角色已存在");
            }
        });
        Role role = new Role();
        role.setRoleName(roleName);
        role.setCreateTime(System.currentTimeMillis());
        role.setRoleValue(UUID.randomUUID() + "");
        if (this.insert(role)) {
            String mIds[] = menuId.split(",");
            for (int i = 0; i < mIds.length; i++) {
                Long mid = Long.valueOf(mIds[i]);
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setmId(mid);
                roleMenu.setrId(role.getId());
                if (!roleMenuService.insert(roleMenu)) {
                    throw new CommonException("保存角色失败");
                }
            }
        }
        jedisUtil.update("roles", this.selectByMap(new HashMap<>()),0);
        return ResponseUtil.getSuccessMap();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> delRole(Long roleId) {
        Map map = new HashMap();
        map.put("role_id", roleId);
        if(roleUserService.selectByMap(map).size()>0){
            throw new CommonException("不可删除该角色，该角色下还有其他用户的存在");
        }
        map.clear();
        map.put("r_id",roleId);
        if (roleMenuService.deleteByMap(map)) {
            if (this.deleteById(roleId)) {
                jedisUtil.update("roles", this.selectByMap(new HashMap<>()),0);
                return ResponseUtil.getSuccessMap();
            }
        }
        throw new CommonException("删除失败");
    }
}
